package AntlrParser;
import java.beans.Expression;
import java.io.IOException;

import javax.swing.text.html.HTMLDocument.HTMLReader;

import org.antlr.analysis.*;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenRewriteStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.Tree;

public class HqlParserMain {
	
	public static int level = 0;

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ANTLRFileStream fs = new ANTLRFileStream("testSql2.txt");
		HbaseSQLLexer lex = new HbaseSQLLexer(fs);
		TokenRewriteStream tokens = new TokenRewriteStream(lex);
		HbaseSQLParser grammar = new HbaseSQLParser(tokens);
		CommonTree pt=null;
		try {
			pt = (CommonTree)grammar.start().getTree();
		} catch (RecognitionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		visitGrammarTree(pt);
	}
	
	public static void visitGrammarTree(CommonTree g)
	{
		for(int i=0; i < level; i++)
		{
			System.out.print("\t");
		}
		level++;
//		System.out.println(g.toString());
		if(g==null)
		{
//			System.out.println("Tree is Null");
			return;
		}
		else
		{
			if(g.token!=null)
				System.out.println(g.token.getText());
		}
		System.out.print("\t");
		for(int i=0; i < g.getChildCount(); i++)
		{
			Tree temp = g.getChild(i);
			visitGrammarTree((CommonTree) temp);
		}
		level--;
//		System.out.println();
	}

}
